package com.appcenter.message.service;


import com.appcenter.message.input.externalapi.SendMsgInput;
import com.appcenter.message.output.externalapi.SendMsgOutput;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import java.io.IOException;

@Service
public class MQProducerService {
    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    MessagesService msgService;

    /**
     *  创建人:Coke
     *  备注: 进消息队列
     *  时间: 2019/4/26 14:51 
     *  参数: 
     */
    public void sendMessage(final SendMsgInput input) throws JsonProcessingException {
        String sendMsgText = objectMapper.writeValueAsString(input);
        System.out.println("开始发送数据:" + sendMsgText);
        Destination destination = new ActiveMQQueue("sendmsg.queue");
        jmsTemplate.convertAndSend(destination, sendMsgText);
    }
    @JmsListener(destination = "sendmsg.queue")
    public void consumerMessage(String text) throws Exception {
        System.out.println("开始发送数据:" + text);
        SendMsgInput input = objectMapper.readValue(text, SendMsgInput.class);
        SendMsgOutput output=msgService.SendMessage(input);
        //System.out.println("异步发送消息" + output.getItem().getSendResultState());
    }
}
